/*
 * bomb_list.c
 *
 *  Created on:  2013
 *      Author: Eddy Zogbo and Moncef Sbay
 */

#include <stdio.h>
#include <stdlib.h>
#include <assert.h>
#include <stddef.h>
#include <SDL.h>
#include <SDL_image.h>

#include "../include/game.h"
#include "../include/misc.h"
#include "../include/bomb.h"

#include "../include/bomb_list.h"

//Bomb linked list
struct cell {
	t_bomb bomb;
	t_cell next;
	t_cell previous;
};

//the bomb_list is represented by the first cell
struct bomb_list {
	t_cell first;
};

t_bomb_list bomb_list_init(t_bomb bomb) {
	assert(bomb);
	t_bomb_list bomb_list = malloc(sizeof(*bomb_list));
	t_cell cell = malloc(sizeof(*cell));
	if (!bomb_list || !cell)
		error("Memory error");
	cell->bomb = bomb;
	cell->next = NULL;
	cell->previous = NULL;

	bomb_list->first = cell;
	return bomb_list;
}

void list_add(t_bomb_list bomb_list, t_bomb bomb) {
	assert(bomb_list);
	assert(bomb);
	t_cell cell = malloc(sizeof(*cell));
	if (!cell)
		error("Memory error");

	cell->bomb = bomb;
	cell->previous = NULL;
	cell->next = bomb_list->first;

	bomb_list->first->previous = cell;
	bomb_list->first = cell;
}

t_cell get_first(t_bomb_list bomb_list) {
	assert(bomb_list);

	return bomb_list->first;
}

t_cell get_end(t_bomb_list bomb_list) {
	assert(bomb_list);
	t_cell key = get_first(bomb_list);
	t_cell key1 = key;
	while (get_next_cell(key) != NULL) {
		key1 = key;
		key = get_next_cell(key);
	}
	return key1;
}

t_bomb get_bomb(t_cell cell) {
	assert(cell);

	return cell->bomb;
}

t_cell get_next_cell(t_cell cell) {
	assert(cell);

	return cell->next;
}

t_cell get_previous_cell(t_cell cell) {
	assert(cell);

	return cell->previous;
}
//read through the bomb_list and delete one specific bomb
void bomb_list_delete(t_bomb_list bomb_list, t_bomb bomb) {

	assert(bomb_list);

	if (bomb_list) {
		t_cell key = bomb_list->first;

		while (get_next_cell(key) != NULL) {
			if (key->bomb == bomb) {

				if (key->previous == NULL) {
					bomb_list->first = get_next_cell(key);
					bomb_list->first->previous = NULL;
				}

				else if (key->next == NULL) {
					key->previous->next = NULL;
					if (key->previous == NULL) {
						bomb_list->first = NULL;
						break;
					}
				}

				else {
					key->previous->next = key->next;
					key->next->previous = key->previous;
				}
				free(key);
			} else
				key = get_next_cell(key);
		}
	}
}
//read through the bomb_list and delete bomb that already exploded
void bomb_list_update(t_bomb_list bomb_list) {
	assert(bomb_list);
	t_cell key = get_end(bomb_list);
	while (key != NULL) {
		if (has_exploded(get_bomb(key)) == 1) {
			bomb_list_delete(bomb_list, get_bomb(key));
		}
		key = get_previous_cell(key);
	}
}
//read through the bomb_list and display each bomb
void bomb_list_display(t_bomb_list bomb_list, t_map map, t_player player,
		t_monster_list monster_list, SDL_Surface *screen) {
	assert(bomb_list);
	assert(screen);
	assert(map);
	assert(player);
	assert(monster_list);
	t_cell key = get_first(bomb_list);
	while (get_next_cell(key) != NULL) {
		bomb_display(get_bomb(key), map, player, monster_list, screen);
		key = get_next_cell(key);
	}
}

